using System;
using FireStarter.Base;
using Firestarter.DB.Schema.BO;
using Firestarter.DB.Schema.Test.BO.TestUtils;
using FireStarterModeller.Test;
using NUnit.Framework;

namespace Firestarter.DB.Schema.Test.BO
{
    [TestFixture]
    public partial class TestDBView : TestBase
    {

        [Test]
        public void Test_ToString()
        {
            //---------------Set up test pack-------------------
            DBView dbView = TestUtilsDBView.CreateUnsavedValidDBView();
            //---------------Assert Precondition----------------
            Assert.IsFalse(String.IsNullOrEmpty(dbView.TableName), "TableName Should not be null");
            //---------------Execute Test ----------------------
            string actualToString = dbView.ToString();
            //---------------Test Result -----------------------
            Assert.AreEqual(dbView.TableName, actualToString);
        }
        [Test]
        public void Test_ToString_TableNameNull()
        {
            //---------------Set up test pack-------------------
            DBView dbView = TestUtilsDBView.CreateUnsavedValidDBView();
            dbView.TableName = null;
            //---------------Assert Precondition----------------
            Assert.IsTrue(String.IsNullOrEmpty(dbView.TableName), "TableName Should be null");
            //---------------Execute Test ----------------------
            string actualToString = dbView.ToString();
            //---------------Test Result -----------------------
            Assert.AreEqual(dbView.ViewID.ToString(), actualToString);
        }
        [Test]
        public void Test_ToString_TableNameNull_TableIDNull()
        {
            //---------------Set up test pack-------------------
            DBView dbView = TestUtilsDBView.CreateUnsavedValidDBView();
            dbView.TableName = null;
            dbView.ViewID = null;
            //---------------Assert Precondition----------------
            Assert.IsTrue(String.IsNullOrEmpty(dbView.TableName), "TableName Should be null");
            Assert.IsNull(dbView.ViewID);
            //---------------Execute Test ----------------------
            string actualToString = dbView.ToString();
            //---------------Test Result -----------------------
            Assert.AreEqual("-unnamed View-", actualToString);
        }

        [Test]
        public void Test_DefaultPropertyValues()
        {
            //---------------Set up test pack-------------------

            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            DBView dbView = new DBView();
            //---------------Test Result -----------------------
            Assert.AreEqual(ChangeType.None, dbView.PendingChangeType);
        }

        [Test]
        public void TestGetColumnInfo_NullWhenNoColumnsExist()
        {
            //---------------Set up test pack-------------------
            DBView dbView = new DBView();
            //---------------Assert Precondition----------------
            Assert.AreEqual(0, dbView.Columns.Count);
            //---------------Execute Test ----------------------
            string nonExistentColumn = TestUtilsShared.GetRandomString();
            DBViewColumn dbColumn = dbView.GetColumn(nonExistentColumn);
            //---------------Test Result -----------------------
            Assert.IsNull(dbColumn);
        }

        [Test]
        public void TestGetColumnInfo_NullIfNotFound()
        {
            //---------------Set up test pack-------------------
            DBView dbView = new DBView();

            DBViewColumn firstDBViewColumn = new DBViewColumn();
            firstDBViewColumn.ColumnName = "TestColumn1";
            dbView.Columns.Add(firstDBViewColumn);

            DBViewColumn secondDBViewColumn = new DBViewColumn();
            secondDBViewColumn.ColumnName = "TestColumn2";
            dbView.Columns.Add(secondDBViewColumn);
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            string nonExistentColumn = TestUtilsShared.GetRandomString();
            DBViewColumn dbColumn = dbView.GetColumn(nonExistentColumn);
            //---------------Test Result -----------------------
            Assert.IsNull(dbColumn);
        }

        [Test]
        public void TestGetColumnInfo_ReturnsCorrectColumnInfoIfExists()
        {
            //---------------Set up test pack-------------------
            DBView dbView = new DBView();

            DBViewColumn firstDBViewColumn = new DBViewColumn();
            firstDBViewColumn.ColumnName = "TestColumn1";
            dbView.Columns.Add(firstDBViewColumn);

            DBViewColumn secondDBViewColumn = new DBViewColumn();
            secondDBViewColumn.ColumnName = "TestColumn2";
            dbView.Columns.Add(secondDBViewColumn);
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            string existentColumnName = "TestColumn1";
            DBViewColumn foundDBViewColumn = dbView.GetColumn(existentColumnName);
            //---------------Test Result -----------------------
            Assert.IsNotNull(foundDBViewColumn);
            Assert.AreEqual(existentColumnName, foundDBViewColumn.ColumnName);
        }


        //[Test]
        //public void Test_GetClassName_ShouldConvertDBViewNameToClassName()
        //{
        //    //---------------Set up test pack-------------------
        //    DBView dbView = new DBView("   TBLmy_table names ");
        //    DMSolution solution = CreateDMSolution();
        //    DBToDMClassImporter dbToDMClassImporter = new DBToDMClassImporter(solution);
        //    dbToDMClassImporter.TableNamePrefix = "tbl";
        //    //---------------Assert Precondition----------------
        //    //---------------Execute Test ----------------------
        //    DMClass dmClass = dbToDMClassImporter.Import(dbView);
        //    //---------------Test Result -----------------------
        //    Assert.IsNotNull(dmClass);
        //    Assert.AreEqual("MyTableName", dmClass.ClassNameBO);
        //}

        [Test]
        public void Test_GetClassName_WhenNameFromTableNameAlreadyPascalCased_ShouldReturnNameFromTableName()
        {
            //---------------Set up test pack-------------------
            DBView dbView = new DBView("tbmyClass");
            //---------------Assert Precondition---------------

            //---------------Execute Test ----------------------
            string className = dbView.GetClassName("tb");
            //---------------Test Result -----------------------
            Assert.AreEqual("MyClass", className);
        }

        [Test]
        public void Test_GetClassName_WhenNameFromTableNameNotPascalCased_WithCompositePK_ShouldReturnNameFromTableName()
        {
            //---------------Set up test pack-------------------
            DBView dbView = new DBView("tbmyclass");
            //---------------Assert Precondition---------------
            //---------------Execute Test ----------------------
            string className = dbView.GetClassName("tb");
            //---------------Test Result -----------------------
            Assert.AreEqual("Myclass", className);
        }

    }
}